$(function () {
    fy(1, 3);
})

function tb() {
    $(".table").append("  \t  <tr>\n" +
        "        <td style=\"text-align:left; padding:19px 0;padding-left:20px;\"><input type=\"checkbox\" id=\"checkall\"/>\n" +
        "          全选 </td>\n" +
        "        <td colspan=\"7\" style=\"text-align:left;padding-left:20px;\"><a href=\"javascript:;\" style=\"padding:5px 15px; margin:0 10px;\" class=\"button border-blue icon-edit\" <!--onclick=\"sorts()\"-->> 同意</a><a href=\"javascript:;\" class=\"button border-red icon-trash-o\" style=\"padding:5px 15px;\" <!--onclick=\"DelSelect()\"-->> 否决</a>\n" +
        "         </td>\n" +
        "      </tr>\n" +
        "  <tr>\n" +
        "                <th width=\"100\" style=\"text-align:left; padding-left:20px;\">ID</th>\n" +
        "                <th width=\"10%\">上传人</th>\n" +
        "                <th>资料链接</th>\n" +
        "                <th>题目名称</th>\n" +
        "                <th width=\"10%\">申请时间</th>\n" +
        "                <th width=\"310\">操作</th>\n" +
        "            </tr>")
}

//全选
function qx() {
    $("#checkall").click(function () {
        $("input[name='id[]']").each(function () {
            if (this.checked) {
                this.checked = false;
            } else {
                this.checked = true;
            }
        });
    })
}

function fy(pageNum, pageSize) {
    $.post("/beyond/selectTopic", {"pageNum": pageNum, "pageSize": pageSize}, function (data) {
        $.each(data.list, function (i, cha) {
            $(".table").append(" <tr>\n" +
                "                    <td style=\"text-align:left; padding-left:20px;\"><input type=\"checkbox\" name=\"id[]\" value=\"\"/>\n" +
                "                        " + (i + 1) + "\n" +
                "                    </td>\n" +
                "                    <td>" + cha.name + "</td>\n" +
                "                    <td width=\"10%\"><a target=\"_blank\" style=\"text-decoration:underline;\">" + cha.taecher_topicPath + "</a></td>\n" +
                "                    <td>" + cha.taecher_topicName + "</td>\n" +
                "                    <td>" + cha.taecher_topicDate + "</td>\n" +
                "                    <td>\n" +
                "                        <div class=\"button-group\"><a class=\"button border-main ty\" href=\"javascript:;\" data-id='" + cha.id + "'><span\n" +
                "                                class=\"icon-edit\"></span>同意</a> <a class=\"button border-red bty\" href=\"javascript:;\"  data-id='" + cha.id + "'><span\n" +
                "                                class=\"icon-trash-o\"></span> 否决</a></div>\n" +
                "                    </td>\n" +
                "                </tr>")
        })
        $(".table").append(" <tr>\n" +
            "                <td colspan=\"9\"><div class=\"pagelist\"></div></td>\n" +
            "            </tr>")

        /*分页*/
        $(".pagelist").empty();
        if (data.pageNum > 1) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="1" >首页</a>')
        }
        if (data.pageNum - 1 >= 1) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + (data.pageNum - 1) + '" >上一页</a>')
        }
        $.each(data.navigatepageNums, function (index, fy) {
            if (data.pageNum == fy) {
                $(".pagelist").append(
                    "<a href=\"javascript:void(0)\" style='background-color: skyblue' class=\"curs\" id='" + fy + "'>" + fy + "</a>"
                )
            } else {
                $(".pagelist").append(
                    "<a href=\"javascript:void(0)\" class=\"curs\" id='" + fy + "'>" + fy + "</a>"
                )

            }

        })
        if (data.pageNum < data.pages) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + (data.pageNum + 1) + '" >下一页</a>')
        }
        if (data.pageNum < data.pages) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + data.pages + '" >末页</a>')
        }
        $(".curs").click(function () {
            $(".table").empty();
            tb();
            fy($(this).attr("id"), 3)
            qx();
        })
        $(".ty").click(function () {
            var id = $(this).data("id");
            $.post("/beyond/updateTopicTy", {"id": id}, function (data) {
                fy(1, 3);
                $(".table").empty();
            })
        })
        $(".bty").click(function () {
            var id = $(this).data("id");
            $.post("/beyond/updateTopicBty", {"id": id}, function (data) {
                fy(1, 3);
                $(".table").empty();
            })
        })

        var dd = data.list == 0;
        console.log(dd)
        if (dd) {
            $(".table").empty();
            $(".table").append("<tr>\n" +
                "            <td style='font-size: 16px;color: red'><span style='text-align: center'>暂无数据</span></td>\n" +
                "        </tr>")
        }
    })
}